home *** CD-ROM | disk | FTP | other *** search
/ Programming Microsoft Visual Basic .NET / Programming Microsoft Visual Basic .NET (Microsoft Press)(X08-78517)(2002).bin / setup / vbnet / 23 web forms and controls / firstwebforms / autopostbackform.aspx.vb < prev    next >
Encoding:
Text File  |  2002-03-17  |  3.3 KB  |  89 lines

  1. Imports System.Data.OleDb
  2.  
  3. ' this page demonstrates the AutoPostBack feature of the
  4. ' DropDownList control
  5.  
  6. Public Class WebForm2
  7.     Inherits System.Web.UI.Page
  8.     Protected WithEvents ddlStates As System.Web.UI.WebControls.DropDownList
  9.     Protected WithEvents dgPublishers As System.Web.UI.WebControls.DataGrid
  10.     Protected WithEvents lblCount As System.Web.UI.WebControls.Label
  11.     Protected WithEvents Label1 As System.Web.UI.WebControls.Label
  12.  
  13. #Region " Web Form Designer Generated Code "
  14.  
  15.     'This call is required by the Web Form Designer.
  16.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
  17.  
  18.     End Sub
  19.  
  20.     Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
  21.         'CODEGEN: This method call is required by the Web Form Designer
  22.         'Do not modify it using the code editor.
  23.         InitializeComponent()
  24.     End Sub
  25.  
  26. #End Region
  27.  
  28.     Dim cn As New OleDbConnection(BiblioConnString)
  29.  
  30.     Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  31.         ' fill the state list only the first time the page is requested
  32.         If Not Page.IsPostBack Then
  33.             InitializeStateList()
  34.         End If
  35.  
  36.         ' Number of requests to this page posted so far.
  37.         Dim count As Integer
  38.         If Not Me.ViewState("count") Is Nothing Then
  39.             count = CInt(Me.ViewState("count")) + 1
  40.         End If
  41.         ' Store the value back in the ViewState dictionary.
  42.         Me.ViewState("count") = count
  43.         ' Display in a Label control.
  44.         lblCount.Text = count.ToString
  45.     End Sub
  46.  
  47.     ' Display unique state strings in the ddlStates DropDownList control.
  48.  
  49.     Sub InitializeStateList()
  50.         cn.Open()
  51.         Dim cmd As New OleDbCommand("SELECT DISTINCT State FROM Publishers", cn)
  52.         Dim dr As OleDbDataReader = cmd.ExecuteReader()
  53.         ddlStates.DataSource = dr
  54.         ' The field used for filling the list are of the DropDownList control.
  55.         ddlStates.DataTextField = "State"
  56.         ddlStates.DataBind()
  57.         ' close the DataReader and the Connection
  58.         dr.Close()
  59.         cn.Close()
  60.     End Sub
  61.  
  62.     ' display all states or only a subset of all states in the DataGrid
  63.     ' depending on which element is selected in the DropDownList
  64.  
  65.     Private Sub ShowPublishers()
  66.         ' Create the query string.
  67.         Dim sql As String = "SELECT Name, Address, City, State FROM Publishers"
  68.         If ddlStates.SelectedIndex >= 0 Then
  69.             sql &= " WHERE State = '" & ddlStates.SelectedItem.Text & "'"
  70.         End If
  71.  
  72.         ' Display All publishers in the DataGrid.
  73.         If cn.State <> ConnectionState.Open Then cn.Open()
  74.         Dim cmd As New OleDbCommand(sql, cn)
  75.         Dim dr As OleDbDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
  76.         dgPublishers.DataSource = dr
  77.         dgPublishers.DataBind()
  78.         dr.Close()
  79.     End Sub
  80.  
  81.     ' when a new state is selected in the DropDownList control, it
  82.     ' causes a postback and this routine fires
  83.  
  84.     Private Sub ddlStates_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlStates.SelectedIndexChanged
  85.         ShowPublishers()
  86.     End Sub
  87.  
  88. End Class
  89.